<template>
	<view class="editbook">
		<view class="bg">
			<view class="book-box">
				<view class="book" v-for="(item,index) in user.bookstore">
					<view class="img-box">
						<image :src="item.link[1].href" mode="widthFix"></image>
					</view>
					<view class="book-title">
						{{item.title}}
					</view>
					<view class="author">
						{{item.author.name}}
					</view>
					<view class="del" @click="delBook(item.id)">
						<image src="../../static/del.png" mode="widthFix"></image>
					</view>
				</view>
			</view>





			<!-- <view class="bottom">
			
		</view> -->
		</view>
	</view>
</template>

<script>
	export default {
		name: 'editbook',
		data() {
			return {
				userInfo: [],
				user: [],
			};
		},
		methods: {
			getBookstore() {
				let user = uni.getStorageSync('userInfo');
				this.userInfo = JSON.parse(user);
				uniCloud.callFunction({
					name: 'getUser',
					data: {
						phone: this.userInfo.phone
					}
				}).then(res => {
					console.log('res ==> ', res);
					this.user = res.result.data[0];
					console.log('this.user ==> ', this.user);
					uni.hideLoading()
				}).catch(err => {
					console.log('err ==> ', err);
				})
			},
			delBook(bookId) {
				// console.log(bookId);
				let bookList = this.user.bookstore
				let index = bookList.findIndex((item) => item.id === bookId);
				let user = uni.getStorageSync('userInfo');
				let userInfo = JSON.parse(user);
				uni.showModal({
					title: '您确定删除这本书籍吗?',
					success(res) {
						if (res.confirm) {
							uni.showToast({
								title: '删除成功',
								icon: 'success'
							})
							bookList.splice(index, 1);
							uniCloud.callFunction({
								name: 'updataBook',
								data: {
									phone: userInfo.phone,
									bookstore: bookList,
								}
							}).then(res => {
								console.log('res ==> ', res);
							}).catch(err => {
								console.log('err ==> ', err);
							})

						} else if (res.cancel) {
							// uni.showToast({
							// 	title: '取消删除',
							// 	icon: 'none'
							// })
						}
					}
				})

			}
		},
		onLoad() {
			uni.showLoading({
				title: '加载中'
			});
			this.getBookstore();
		}
	}
</script>

<style lang="scss">
	.bg {
		width: 100%;
		// height: 1234rpx;
		height: 100vh;
		background-image: url('../../static/background-image.png');
		background-repeat: no-repeat;
	}

	.book-box {
		display: flex;
		flex-wrap: wrap;
		background-color: #FCECCC;
		min-height: 100vh;

		.book {
			width: 200rpx;
			height: 370rpx;
			// border: 1px solid #EB732A;
			margin-left: 36rpx;
			margin-top: 36rpx;
			position: relative;

			// background-color: yellow;
			.img-box {
				width: 200rpx;
				height: 260rpx;
				border: 1px solid #EB732A;
				border-radius: 20rpx;

				image {
					width: 100%;
					display: block;
					border-radius: 20rpx;
				}
			}

			.book-title {
				font-size: 32rpx;
				font-weight: 700;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 1;
				-webkit-box-orient: vertical;
				margin-top: 10rpx;
			}

			.author {
				font-size: 26rpx;
				color: #a8a8a8;
				margin-top: 8rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 1;
				-webkit-box-orient: vertical;
			}

			.del {
				width: 60rpx;
				height: 60rpx;
				// background-color: red;
				position: absolute;
				top: -10px;
				right: -10px;

				image {
					width: 100%;
					display: block;
				}
			}
		}
	}

	// .bottom{
	// 	width: 100%;
	// 	height: 100rpx;
	// 	background-color: #F0CEAD;
	// 	position: fixed;
	// 	bottom: 0;
	// }
</style>